package com.mccken.demo.io.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-11-06 12:23
 **/
public class NIO_6 {

	/**
	 * 从channel中读取数据到多个buffer中,先读满第一个buffer,再读满第二个buffer
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		ScatteringByteChannel channel = FileChannel.open(Paths.get("/Users/mccken/文档/sublime/1.txt"), StandardOpenOption.READ);
		ByteBuffer buffer1 = ByteBuffer.allocate(100);
		ByteBuffer buffer2 = ByteBuffer.allocate(10);
		ByteBuffer[] buffers = {buffer1, buffer2};
		while (channel.read(buffers) != -1) {
			buffer1.flip();
			buffer2.flip();

			System.out.println("buffer1: " + new String(buffer1.array()));
			System.out.println("buffer2: " + new String(buffer2.array()));
			
			buffer1.clear();
			buffer2.clear();
		}
		
		channel.close();
	}
}
